//
//  HDSyncDataVC.m
//  HDSmartPenSDKDemo
//
//  Created by Lida on 2017/7/17.
//  Copyright © 2017年 牛的mini. All rights reserved.
//

#import "HDSyncDataVC.h"
#import "HDBeaconManager.h"
@interface HDSyncDataVC ()<UITableViewDataSource>
@property (nonatomic, strong) HDBeaconManager *beaconManager;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation HDSyncDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"同步数据";
    _dataArray = [NSMutableArray arrayWithCapacity:0];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.rowHeight =80;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData:) name:kBeacon_SMPen_Data_Notification object:nil];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [LCCoolHUD showLoading:@"同步数据" inView:self.view];
    _beaconManager = [HDBeaconManager sharedBeacon];
    [_beaconManager syncData];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)loadData:(NSNotification *)noti{
   
    NSArray *datas = noti.object;
    [_dataArray addObjectsFromArray:datas];
    [LCCoolHUD hideInView:self.view];
    [_tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = [_dataArray objectAtIndex:indexPath.row];
    NSString *name = [dic objectForKey:@"e_id"];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    label1.text = name;
    [cell.contentView addSubview:label1];
    NSString *date = [NSString stringWithFormat:@"时间：%@号%@时%@分",[dic objectForKey:@"d"],[dic objectForKey:@"h"],[dic objectForKey:@"i"]];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, ScreenWidth, 40)];
    label2.text = date;
    [cell.contentView addSubview:label2];

    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
